import { gameConfigHandle } from "js/config/gameConfig"
import { PlayerModel } from "js/model/playerModel"
import utils from "./utils"

class GameUtils {
    getNeedResource(need, lvl) {
        let needNodes = []
        let needs = Object.entries(need)
        for (let i = 0; i < needs.length; i++) {
            let need = needs[i]
            let needType = need[0]
            let needValues: any = need[1]
            let needAmount = utils.getIncValue(needValues, lvl - 1)
            needNodes.push([needType, needAmount])
        }
        return needNodes
    }

    getMapMax(mapProgressMax: number) {
        // let mapID = Math.floor(mapProgressMax / 100000000000) + 100000
        let _floor = Math.floor(mapProgressMax % 1000000000000 / 100000000)
        let c = _floor / 10 
        let f = _floor % 10
        if (c < 1) {
            c = 1
        } else {
            c = Math.floor(c) + (f == 0 ? 0 : 1)
        }
        if (f == 0 && _floor != 0) {
            f = 10
        } else if (_floor == 0) {
            f = 1
        }
        let mapID = 100000 + (c > 22 ? 22 : c)
        let floor = _floor > 210 ? _floor - 210 : f
        return { mapID, floor }
    }

    setMapMax(player: PlayerModel, newFloor: number, time: number) {
        let oriMapProgressMax = player.baseInfo.mapProgressMax || 1000000000000 + 100000000 + Math.floor((2524608000000 - time) / 60000)
        let { mapID, floor } = this.getMapMax(oriMapProgressMax)
        //2524608000000 - 2050year
        if (newFloor > floor) {
            let newMapProgressMax = 1000000000000 + newFloor * 100000000 + Math.floor((2524608000000 - time) / 60000)
            player.baseInfo.mapProgressMax = newMapProgressMax
            return newMapProgressMax
        } else {
            return null
        }
    }

    geDevilProgressMax(mapProgressMax: number) {
        let floor = Math.floor(mapProgressMax / 100000000)
        return { floor }
    }

    isLimit(player: PlayerModel, floor: number, reviewVersion: boolean = false) {
        if (reviewVersion) {
            return false
        }
        let currentMax = 1000000000000 + floor * 100000000
        if (player.baseInfo.mapProgressMax < currentMax) {
            return true
        } else {
            return false
        }
    }
    setPlayerTestInfo(player: PlayerModel, key, addValue, addTime) {
        if (!player.info[key]) {
            player.info[key] = { v: 0, t: 0 }
        }
        player.info[key].v += addValue
        player.info[key].t += addTime
    }
}


let gameUtils = new GameUtils()
export default gameUtils